package com.ktkj.init.controller;

import com.ktkj.init.constant.ModelConstant;
import com.ktkj.init.entity.Attachment;
import com.ktkj.init.response.Response;
import com.ktkj.init.response.ResponseCallBack;
import com.ktkj.init.response.ResponseCriteria;
import com.ktkj.init.service.AttachmentService;
import com.ktkj.init.swagger.ParamType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;


/**
 * 附件管理.
 *
 * @author yanlixiong
 * @date 2019-05-07
 */
@Api(tags = "附件管理")
//@ApiIgnore
@Controller
@RequestMapping(path = "/attachment")
public class AttachmentController {

    @Resource
    private AttachmentService attachmentService;

    @ApiOperation(value = "浏览器打开文件(下载功能)")
    @GetMapping(path = "/downloadFile")
    public void downloadFile(@RequestParam final String fileName, HttpServletResponse response) {
        attachmentService.downloadFile(fileName, response);
    }

    @ApiOperation(value = "浏览器打开文件(预览功能)")
    @GetMapping(path = "/previewFile")
    public void previewFile(@RequestParam final String fileName, HttpServletResponse response) {
        attachmentService.previewFile(fileName, response);
    }

    @ApiOperation(value = "附件上传至本地")
    @PostMapping(path = "/uploadFileLocalPath", headers = "content-type=multipart/form-data")
    @ResponseBody
    @ApiImplicitParams({@ApiImplicitParam(name = "fileName", value = "文件名", example = "123.png", required = true, paramType = ParamType.FORM)})
    public Response uploadFileLocalPath(@RequestPart("file") final MultipartFile file, @NotBlank(message = "文件名不能为空") @RequestParam final String fileName) {
        return new ResponseCallBack() {
            @Override
            public void execute(ResponseCriteria criteria, Object... obj) {
                Attachment bean = attachmentService.uploadFileLocalPath(file, fileName, ModelConstant.MODE_TEST);
                criteria.addSingleResult(bean);
            }
        }.sendRequest();
    }

    @ApiOperation(value = "附件上传至FTP")
    @PostMapping(path = "/uploadFileFtpPath", headers = "content-type=multipart/form-data")
    @ResponseBody
    @ApiImplicitParams({@ApiImplicitParam(name = "fileName", value = "文件名", example = "123.png", required = true, paramType = ParamType.FORM)})
    public Response uploadFileFtpPath(@RequestPart("file") final MultipartFile file, @NotBlank(message = "文件名不能为空") @RequestParam final String fileName) {
        return new ResponseCallBack() {
            @Override
            public void execute(ResponseCriteria criteria, Object... obj) {
                Attachment bean = attachmentService.uploadFileFtpPath(file, fileName, ModelConstant.MODE_TEST);
                criteria.addSingleResult(bean);
            }
        }.sendRequest();
    }

    @ApiOperation(value = "从FTP下载文件")
    @GetMapping(path = "/downloadFileByFtp")
    @ResponseBody
    public Response downloadFileByFtp(@RequestParam final String ftpFileName) {
        return new ResponseCallBack() {
            @Override
            public void execute(ResponseCriteria criteria, Object... obj) {
                criteria.addSingleResult(attachmentService.downloadFileByFtp(ftpFileName, ModelConstant.MODE_TEST));
            }
        }.sendRequest();

    }
}
